package cn.tedu.myioc;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

//2.初始化list
public class MyIoc {
    private List<Bean> list =new ArrayList<>();
    public MyIoc() throws Exception {
        Bean b1 =new Bean("user","cn.tedu.myioc.User");
        Bean b2 =new Bean("dept","cn.tedu.myioc.Dept");
        Collections.addAll(list,b1,b2);

        createobj();
    }
    private Map<String,Object> map =new ConcurrentHashMap<>();
    //3.进行IOC,核心就是Map里put(bean的name属性的值，是根据bean的path值创建的对象)
    private void createobj() throws Exception {

        for (Bean b:list){
            String k =b.getName();
            Object v =Class.forName(b.getPath()).newInstance();
            map.put(k,v);
        }
    }
    //4.getBean返回创建好的对象
    public Object getBean(String name){

        return map.get(name);
    }
}
